{extend name="base" /}
{block name="body"}
<div class="layui-body">
    <!--tab标签-->
    <div class="layui-tab layui-tab-brief">
        <ul class="layui-tab-title">
            <li class=""><a href="{:url('admin/room/index')}">游戏房间</a></li>
            <li class=""><a href="{:url('admin/room/add')}">添加房间</a></li>
            <li class="layui-this">机器人管理</li>
        </ul>
        <div class="layui-tab-content">
            <div class="layui-tab-item layui-show">


                <table class="layui-table" style="text-align: center">
                    <thead>
                    <tr>
                        <th style="width: 30px;">ID</th>
                        <th>房间ID</th>
                        <th>房间UUID</th>
                        <th>机器人UUID</th>
                        <th>机器人昵称</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    {foreach name="robot_list" item="vo"}
                    <tr>
                        <td>{$vo.rr_id}</td>
                        <td>{$vo.room_id}</td>

                        <td>{$vo.room_uuid}</td>
                        <td>{$vo.user_uuid}</td>
                        <td>{$vo.robot_nick_name}</td>
                        <td>
                            <a href="{:url('admin/room/robot_delete',['id'=>$vo['rr_id']])}"
                               class="layui-btn layui-btn-danger layui-btn-mini ajax-delete"
                               data-msg="确认删除机器人？">删除机器人</a>
                        </td>
                    </tr>
                    {/foreach}
                    <tr>
                        <td colspan="7">
                            <button id="add_robot" class="layui-btn layui-btn-small"><i class="fa fa-plus"></i> 新增机器人
                            </button>
                        </td>
                    </tr>
                    </tbody>
                </table>
                <hr>
                <div class="robot_content" id="robot_content" style="display: none">
                    <table class="layui-table" style="text-align: center">
                        <thead>
                        <tr>
                            <th style="width: 30px;">ID</th>
                            <th>用户名</th>
                            <th>用户昵称</th>
                            <th>用户UUID</th>
                            <th>状态</th>
                            <th>所在房间</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody id="robot_tbody">

                        </tbody>
                    </table>
                </div>

                <!--分页-->
            </div>
        </div>
    </div>
</div>


{/block}
{block name="script"}
<script>
    $('#add_robot').click(function () {
        $.ajax({
            url: '{:url("admin/room/getAjaxRobot")}',
            type: 'get',
            success: function (res) {
                if (res.code === 0) {
                    $('#robot_content').show();
                    renderTmpl('robot_list', 'robot_tbody', {list: res.data});
                }
            }
        });
    });

    function renderTmpl(id, target, data) {
        var getTpl = $('#' + id).html();
        laytpl(getTpl).render(data, function (html) {
            $('#' + target).html(html);
        });
    }

    $(document).on('click', '.mc-ajax', function (e) {
        e.preventDefault();
        var data = $(this).data();
        var url = $(this).attr('href');
        console.log(data);
        $.ajax({
            url: url,
            type: 'post',
            data: {room_id: {$room_id}, robot_id: data['user_id']},
            dataType: 'json',
            success: function (res) {
                if (res.code === 0) {
                    layer.msg(res.msg);
                    return false;
                } else {
                    layer.msg(res.msg);
                    return false;
                }
            }

        })

    })
</script>

<script id="robot_list" type="text/html">
    {{#  layui.each(d.list, function(index, item){ }}
    <tr>
        <td>{{ item.id}}</td>
        <td>{{ item.username}}</td>
        <td>{{ item.user_nick}}</td>
        <td>{{ item.user_uuid}}</td>
        <td>{{# if(item.status == '0'){ }}
            {{ '禁用' }}
            {{# }else{ }}
            {{ '正常' }}
            {{# } }}
        </td>
        <td>{{ item.room_uuid || '无'}}</td>
        <td>
            <button href="{:url('admin/room/robot_add')}/id/{$room_id}" data-user_id="{{item.id}}"
                    class="layui-btn layui-btn-info layui-btn-mini mc-ajax {{# if(item.room_id){ }} {{
            'layui-btn-disabled'}} {{# } }}" {{# if(item.room_id){ }} {{
            'disabled'}} {{# } }}>添加</button>
        </td>
    </tr>
    {{#  }); }}

</script>
{/block}